﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCube
{
    public class Buffer3D
    {
        public int Size;
        public LightColor[,,] Pixels;

        public Buffer3D(int size)
        {
            this.Size = size;

            Pixels = new LightColor[size, size, size];
        }

        public void SetPixel(Point3D point, LightColor color)
        {
            Pixels[point.X, point.Y, point.Z] = color;
        }

        public void SetPixels(Point3D[] points, LightColor color)
        {
            foreach (var point in points)
            {
                SetPixel(point, color);
            }
        }

        public LightColor GetPixcel(Point3D point)
        {
            return Pixels[point.X, point.Y, point.Z];
        }

        public void Clear()
        {
            Point3D pixel = Point3D.Zero;
            for (; pixel.Z < Size; pixel.Z++)
            {
                for (; pixel.Y < Size; pixel.Y++)
                {
                    for (; pixel.X < Size; pixel.X++)
                    {
                        SetPixel(pixel, LightColor.Black);
                    }
                }
            }
        }

        public Buffer3D CopyNew()
        {
            var newBuffer = new Buffer3D(Size);
            Point3D pixel = Point3D.Zero;
            for (; pixel.Z < Size; pixel.Z++)
            {
                for (; pixel.Y < Size; pixel.Y++)
                {
                    for (; pixel.X < Size; pixel.X++)
                    {
                        newBuffer.SetPixel(pixel, GetPixcel(pixel));
                    }
                }
            }
            return newBuffer;
        }

        public void Fuse(Buffer3D buffer)
        {
            Point3D pixel = Point3D.Zero;
            for (; pixel.Z < Size; pixel.Z++)
            {
                for (; pixel.Y < Size; pixel.Y++)
                {
                    for (; pixel.X < Size; pixel.X++)
                    {
                        var newColor = buffer.GetPixcel(pixel);
                        if (newColor != LightColor.Black)
                            SetPixel(pixel, newColor);
                    }
                }
            }
        }
    }
}
